<?php

class Application_Model_Authors extends Application_Model_Base{
    private static $instance = null;
    public static function getInstance(){
        if (null === self::$instance) self::$instance = new self();
        return self::$instance;
    }
    private function __clone() {}
    private function __construct() {}

    public function getPaginatorForFilter($params){
        $select = $this->getDBAdapter()->select()
            ->from('authors');
        if (!empty($params['author_name'])){
            $select->where("`author_name` LIKE '%{$params['author_name']}%'");
        }
        if(!empty($params['author_surname'])){
            $select->where("`author_surname` LIKE '%{$params['author_surname']}%'");
        }
        if(!empty($params['author_patronymic'])){
            $select->where("`author_patronymic` LIKE '%{$params['author_patronymic']}%'");
        }
        if(!empty($params['author_nickname'])){
            $select->where("`author_nickname` LIKE '%{$params['author_nickname']}%'");
        }
        $paginatorAdapter = new Zend_Paginator_Adapter_DbSelect($select);
        return new Zend_Paginator($paginatorAdapter);
    }

    public function getAllForSelect(){
        $authors = $this->getDBAdapter()->query("
            select * from `authors`
        ")->fetchAll();
        $authorsForSelect = array();
        foreach($authors as $author){
            $authorsForSelect[$author['author_id']] = $author['author_surname']
                . ' ' . $author['author_name']
                . ' ' . $author['author_patronymic']
                . ' (' . $author['author_nickname'] . ')';
        }
        return $authorsForSelect;
    }

    public function create($data){
        $this->getDBAdapter()->insert('authors', $data);
    }
}